package com.yanqu.road.entity.activity.jiuzhou.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.jiuzhou.eJiuZhouGameZoneType;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 九州棋局配置
 */
public class JiuZhouConfig {

    private ActivityInfo activityInfo;

    // 轮次配置
    private Map<Integer, JiuZhouRoundConfig> roundConfigMap = new ConcurrentHashMap<>();

    // 技能配置
    private Map<Integer, JiuZhouSkillConfig> skillConfigMap = new ConcurrentHashMap<>();
    // 九州积分
    private int exchangeScoreItemId;
    // 九州币
    private int moneyItemId1;
    // 棋魂
    private int moneyItemId2;
    // 膜拜奖励
    private String worshipReward;
    private int worshipRewardId;
    private int worshipRewardMinNum;
    private int worshipRewardMaxNum;

    // 聊天保留条数
    private int chatKeepNum;
    // 开服日期条件：大于等于的区服参与活动
    private int openLimit;
    // 活动时间（报名期开始|报名期结束|领奖期开始|领奖期结束）
    private List<Integer> jiuZhouTime;
    // 门客数大于等于可报名
    private int sighLimit;
    // 等级大于等于可报名
    private int levelLimit;
    // 对战时间：锦囊选择时间（秒）
    private int chooseSilkTime;
    // 赛区分配条件：已报名玩家赚钱排名千分比
    private List<Integer> zoneCondition;
    // 每个赛场标准人数
    private int courtPlayerNum;
    // 镜像血量
    private int mirrorHp;
    // 两场战斗时间间隔（秒）
    private int fightCd;
    // 两人战斗出战门客数（战斗最大回合）
    private int maxFightRound;
    // 门客出战权重（每个门客相同）
    private int patronsRollWeight;
    // 战场最大回合
    private int maxGameRound;
    // 伤害倍率
    private List<List<Integer>> fightMultipleParamList = new ArrayList<>();
    // 元宝刷锦囊消耗
    private String refreshSkillId3Cost = "";
    // 元宝刷新次数上限
    private int refreshSkillId3Num;

    // 比赛最大轮次
    private int maxRound;

    //每个赛区可以参与的评级
    private Map<Integer,List<Integer>> canJoinZoneByRankGrade = new ConcurrentHashMap<>();

    // 每天首次进入战场界面获得声援券数量
    private int supportNum;

    // 声援券id
    private int moneyItemId3;

    // 每次声援获得声望奖励
    private String supportRepution = "";

    // 声援前4奖励
    private String supportReward = "";

    // 有资格参加的玩家声援奖励
    private String supportSelfReward = "";

    // 能获得奖励的声援对象名次
    private int supportRankNum;

    // 播报所需声援次数
    private int supportBroadcastNum;

    /**
     * 初始化一下活动配置
     */
    public void initConfig(Map<String, ActivityConfig> activityConfigMap) {
        if (activityConfigMap.containsKey(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName())) {
            exchangeScoreItemId = activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_MONEY_ITEM_ID_1.getName())) {
            moneyItemId1 = activityConfigMap.get(eActivityConfigType.NINECHESS_MONEY_ITEM_ID_1.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_MONEY_ITEM_ID_2.getName())) {
            moneyItemId2 = activityConfigMap.get(eActivityConfigType.NINECHESS_MONEY_ITEM_ID_2.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_WORSHIP.getName())) {
            worshipReward = activityConfigMap.get(eActivityConfigType.NINECHESS_WORSHIP.getName()).getValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_CHAT_KEEP_NUM.getName())) {
            chatKeepNum = activityConfigMap.get(eActivityConfigType.NINECHESS_CHAT_KEEP_NUM.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_OPEN_LIMIT.getName())) {
            openLimit = activityConfigMap.get(eActivityConfigType.NINECHESS_OPEN_LIMIT.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_TIME.getName())) {
            jiuZhouTime = activityConfigMap.get(eActivityConfigType.NINECHESS_TIME.getName()).getIntListValue("\\|");
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SIGH_LIMIT.getName())) {
            sighLimit = activityConfigMap.get(eActivityConfigType.NINECHESS_SIGH_LIMIT.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_CHOOSE_SILK_TIME.getName())) {
            chooseSilkTime = activityConfigMap.get(eActivityConfigType.NINECHESS_CHOOSE_SILK_TIME.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_ZONE_CONDITION.getName())) {
            zoneCondition = activityConfigMap.get(eActivityConfigType.NINECHESS_ZONE_CONDITION.getName()).getIntListValue("\\|");
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_COURT_PLAYER_NUM.getName())) {
            courtPlayerNum = activityConfigMap.get(eActivityConfigType.NINECHESS_COURT_PLAYER_NUM.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_LV_LIMIT.getName())) {
            levelLimit = activityConfigMap.get(eActivityConfigType.NINECHESS_LV_LIMIT.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_MIRROR_HP.getName())) {
            mirrorHp = activityConfigMap.get(eActivityConfigType.NINECHESS_MIRROR_HP.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_FIGHT_CD.getName())) {
            fightCd = activityConfigMap.get(eActivityConfigType.NINECHESS_FIGHT_CD.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_FIGHT_PARTON_NUM.getName())) {
            maxFightRound = activityConfigMap.get(eActivityConfigType.NINECHESS_FIGHT_PARTON_NUM.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_FIGHT_PARTON_WEIGHT.getName())) {
            patronsRollWeight = activityConfigMap.get(eActivityConfigType.NINECHESS_FIGHT_PARTON_WEIGHT.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_FIGHT_ROUND_MAX.getName())) {
            maxGameRound = activityConfigMap.get(eActivityConfigType.NINECHESS_FIGHT_ROUND_MAX.getName()).getIntValue();
        }
        List<String> strings = StringUtils.stringToStringList(worshipReward, "=");
        worshipRewardId = StringUtils.string2intSafe(strings.get(0),101000);
        List<Integer> integerList = StringUtils.stringToIntegerList(strings.get(1), ";");
        worshipRewardMinNum = integerList.get(0);
        worshipRewardMaxNum = integerList.get(1);
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_FIGHT_MULTIPLE.getName())) {
            fightMultipleParamList = new ArrayList<>();
            List<String> list1 = activityConfigMap.get(eActivityConfigType.NINECHESS_FIGHT_MULTIPLE.getName()).getStringListValue("\\|");
            for (String s : list1) {
                fightMultipleParamList.add(StringUtils.stringToIntegerList(s, ";"));
            }
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_REFRESH_COST.getName())) {
            refreshSkillId3Cost = activityConfigMap.get(eActivityConfigType.NINECHESS_REFRESH_COST.getName()).getValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_REFRESH_NUM.getName())) {
            refreshSkillId3Num = activityConfigMap.get(eActivityConfigType.NINECHESS_REFRESH_NUM.getName()).getIntValue();
        }

        for (JiuZhouRoundConfig roundConfig : roundConfigMap.values()) {
            maxRound = Math.max(maxRound, roundConfig.getId());
        }

        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_ZONE_CONDITION_2.getName())){
            Map<Integer,List<Integer>> tempCanJoinMap = new ConcurrentHashMap<>();
            String value = activityConfigMap.get(eActivityConfigType.NINECHESS_ZONE_CONDITION_2.getName()).getValue();
            List<String> stringList = StringUtils.stringToStringList(value, "\\|");
            if (stringList.size() != 3){
                return;
            }
            //王者赛区
            List<Integer> kingGrade = StringUtils.stringToIntegerList(stringList.get(0),";");
            tempCanJoinMap.put(eJiuZhouGameZoneType.king.getValue(),kingGrade);
            //宗师
            List<Integer> masterGrade = StringUtils.stringToIntegerList(stringList.get(1),";");
            tempCanJoinMap.put(eJiuZhouGameZoneType.master.getValue(),masterGrade);
            //精英
            List<Integer> eliteGrade = StringUtils.stringToIntegerList(stringList.get(2),";");
            tempCanJoinMap.put(eJiuZhouGameZoneType.elite.getValue(), eliteGrade);

            canJoinZoneByRankGrade = tempCanJoinMap;
        }
        // ！！！后面加的活动配置一定要加containsKey判断，兼容旧活动配置

        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SUPPORT_NUM.getName())) {
            supportNum = activityConfigMap.get(eActivityConfigType.NINECHESS_SUPPORT_NUM.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_MONEY_ITEM_ID_3.getName())) {
            moneyItemId3 = activityConfigMap.get(eActivityConfigType.NINECHESS_MONEY_ITEM_ID_3.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SUPPORT_REPUTION.getName())) {
            supportRepution = activityConfigMap.get(eActivityConfigType.NINECHESS_SUPPORT_REPUTION.getName()).getValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SUPPORT_REWARD.getName())) {
            supportReward = activityConfigMap.get(eActivityConfigType.NINECHESS_SUPPORT_REWARD.getName()).getValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SUPPORT_RANK_NUM.getName())) {
            supportRankNum = activityConfigMap.get(eActivityConfigType.NINECHESS_SUPPORT_RANK_NUM.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SUPPORT_BROADCAST_NUM.getName())) {
            supportBroadcastNum = activityConfigMap.get(eActivityConfigType.NINECHESS_SUPPORT_BROADCAST_NUM.getName()).getIntValue();
        }
        if (activityConfigMap.containsKey(eActivityConfigType.NINECHESS_SUPPORTSLEF_REWARD.getName())) {
            supportSelfReward = activityConfigMap.get(eActivityConfigType.NINECHESS_SUPPORTSLEF_REWARD.getName()).getValue();
        }
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }


    public Map<Integer, JiuZhouRoundConfig> getRoundConfigMap() {
        return roundConfigMap;
    }

    public void setRoundConfigMap(Map<Integer, JiuZhouRoundConfig> roundConfigMap) {
        this.roundConfigMap = roundConfigMap;
    }

    public Map<Integer, JiuZhouSkillConfig> getSkillConfigMap() {
        return skillConfigMap;
    }

    public void setSkillConfigMap(Map<Integer, JiuZhouSkillConfig> skillConfigMap) {
        this.skillConfigMap = skillConfigMap;
    }

    public int getExchangeScoreItemId() {
        return exchangeScoreItemId;
    }

    public int getMoneyItemId1() {
        return moneyItemId1;
    }

    public int getMoneyItemId2() {
        return moneyItemId2;
    }

    public String getWorshipReward() {
        return worshipReward;
    }

    public int getChatKeepNum() {
        return chatKeepNum;
    }

    public int getOpenLimit() {
        return openLimit;
    }
    // 活动时间（报名期开始|报名期结束|领奖期开始|领奖期结束）
    public List<Integer> getJiuZhouTime() {
        return jiuZhouTime;
    }

    public int getSighLimit() {
        return sighLimit;
    }

    public int getChooseSilkTime() {
        return chooseSilkTime;
    }

    public List<Integer> getZoneCondition() {
        return zoneCondition;
    }

    public int getCourtPlayerNum() {
        return courtPlayerNum;
    }

    public int getActivityId() {
        return activityInfo.getActivityId();
    }

    public JiuZhouSkillConfig getSkillConfig(int skillId) {
        return skillConfigMap.get(skillId);
    }

    public int getLevelLimit() {
        return levelLimit;
    }

    public int getMirrorHp() {
        return mirrorHp;
    }

    public int getFightCd() {
        return fightCd;
    }

    public int getMaxFightRound() {
        return maxFightRound;
    }

    public int getPatronsRollWeight() {
        return patronsRollWeight;
    }

    public int getWorshipRewardId() {
        return worshipRewardId;
    }

    public int getWorshipRewardMinNum() {
        return worshipRewardMinNum;
    }

    public int getWorshipRewardMaxNum() {
        return worshipRewardMaxNum;
    }

    public int getMaxGameRound() {
        return maxGameRound;
    }

    public List<List<Integer>> getFightMultipleParamList() {
        return fightMultipleParamList;
    }

    public String getRefreshSkillId3Cost() {
        return refreshSkillId3Cost;
    }

    public int getRefreshSkillId3Num() {
        return refreshSkillId3Num;
    }

    public int getMaxRound() {
        return maxRound;
    }

    public int getSupportNum() {
        return supportNum;
    }

    public int getMoneyItemId3() {
        return moneyItemId3;
    }

    public String getSupportRepution() {
        return supportRepution;
    }

    public String getSupportReward() {
        return supportReward;
    }

    public int getSupportRankNum() {
        return supportRankNum;
    }

    public int getSupportBroadcastNum() {
        return supportBroadcastNum;
    }

    public String getSupportSelfReward() {
        return supportSelfReward;
    }

    /**
     * 获取某个实力评定等级可以参与的
     */
    public int getCanJoinZoneIdByMonthRankGrade(int rankGrade){
        if (canJoinZoneByRankGrade == null || canJoinZoneByRankGrade.isEmpty()){
            return 0;
        }
        for (Map.Entry<Integer,List<Integer>> entry:canJoinZoneByRankGrade.entrySet()){
            List<Integer> grades = entry.getValue();
            if (grades.contains(rankGrade)){
                return entry.getKey();
            }
        }
        return 0;
    }

    /**
     * 检测某一个赛区是否开启比赛
     */
    public boolean isGameZoneOpen(int gameZoneType){
        if (canJoinZoneByRankGrade == null || canJoinZoneByRankGrade.isEmpty()){
            return false;
        }
        if (!canJoinZoneByRankGrade.containsKey(gameZoneType)){
            return false;
        }
        if (canJoinZoneByRankGrade.get(gameZoneType).isEmpty()){
            return false;
        }
        if (canJoinZoneByRankGrade.get(gameZoneType).size() == 1 && canJoinZoneByRankGrade.get(gameZoneType).get(0) == -1){
            return false;
        }
        return true;
    }

    /**
     * 获取当前一共开了多少个赛区
     */
    public int getOpenGameZoneNum(){
        int totalOpenZone = 0;
        if (canJoinZoneByRankGrade == null || canJoinZoneByRankGrade.isEmpty()){
            return totalOpenZone;
        }

        for (Map.Entry<Integer,List<Integer>> entry:canJoinZoneByRankGrade.entrySet()){
            List<Integer> grades = entry.getValue();
            if (grades.isEmpty()){
                continue;
            }
            if (grades.size() == 1 && grades.get(0) == -1){
                continue;
            }
            totalOpenZone++;
        }
        return totalOpenZone;
    }


}
